import math
import numpy

def create(parent, root):    
    return BaseCalc(parent)

class BaseCalc:    
    def __init__(self, parent):
        self.parent = parent
    
    def SubtractPoints(self, a, b):
        slotlijst = []
        try:
            for i in range(0, len(a)):
                slotlijst.append(a[i]-b[i])
        except TypeError:
            return (a-b)
        return slotlijst
    def AddPoints(self, a, b): 
        slotlijst = []
        try:
            for i in range(0, len(a)):
                slotlijst.append(a[i]+b[i])
        except TypeError:
            return (a-b)
        return slotlijst
    def MultiplyVector(self, a, b):
        c = []
        try:
            for i in range(0,len(a)):
                c[len(c):] = [a[i]*b]
        except TypeError:
            return (a*b)
        return c
    def MultiplyVector_int(self, a, b):
        c = []
        for i in range(0,len(a)):
            c[len(c):] = [int(a[i]*b)]
        return c     
    def Average2Points(self, a, b):
        return [(a[0]+b[0])/2,(a[1]+b[1])/2,(a[2]+b[2])/2]
    def Average2Points_int(self, a, b):
        return [int((a[0]+b[0])/2),int((a[1]+b[1])/2),int((a[2]+b[2])/2)]
    def Average3Points(self, a, b, c):
        return [(a[0]+b[0]+c[0])/3,(a[1]+b[1]+c[1])/3,(a[2]+b[2]+c[2])/3]
    def WeightedAverage2Points(self, a, b, w, y):
        return [(w*a[0]+y*b[0])/(y+w),(w*a[1]+y*b[1])/(y+w),(w*a[2]+y*b[2])/(y+w)]        
    def Length3DVector(self, a):
        return math.sqrt((math.pow(a[0], 2)) + (math.pow(a[1], 2)) + (math.pow(a[2], 2)))
    def Length3DVector2(self, a):
        return (math.pow(a[0], 2)) + (math.pow(a[1], 2)) + (math.pow(a[2], 2))
    def WorldToVoxel(self, a):
        return [int(a[0]/self.volumedata.GetSpacing()[0]), int(a[1]/self.volumedata.GetSpacing()[1]), int(a[2]/self.volumedata.GetSpacing()[2])]
    def VoxelToWorld(self, a):
        return [(a[0]*self.volumedata.GetSpacing()[0]), (a[1]*self.volumedata.GetSpacing()[1]), (a[2]*self.volumedata.GetSpacing()[2])]
    def Normalize(self, a):
        m1 = numpy.matrix([a])
        m1 =  m1 / numpy.linalg.norm(m1)
        return m1.tolist()[0]
    def Multiply3x3MatrixVector(self, matrix, b):
        output1 = [0,0,0]
        for j in range(0,3):
            tmp = 0
            for i in range(0,3):
                tmp = tmp + matrix[j][i] * b[i]
            output1[j] = tmp
        return output1
